﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ykglxt
{
    public partial class Form_YPYXQCX : Form
    {
        public Form_YPYXQCX()
        {
            InitializeComponent();
        }

        SqlConnection con;
        SqlDataAdapter sda;
        DataSet ds;

        private void Form_YPYXQCX_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“yKGLXTDataSet.Dic_YPLB”中。您可以根据需要移动或移除它。
            this.dic_YPLBTableAdapter.Fill(this.yKGLXTDataSet.Dic_YPLB);
            // TODO: 这行代码将数据加载到表“yKGLXTDataSet.Dic_YPJX”中。您可以根据需要移动或移除它。
            this.dic_YPJXTableAdapter.Fill(this.yKGLXTDataSet.Dic_YPJX);
            nudJJ.Enabled = false;
            ds = new DataSet();
            try
            {
                string cmdText = "SELECT R1.YPBM AS 药品编码, YP.MC AS 药品名称, YP.GG AS 规格, YP.BZDW AS 单位, ISNULL(R1.YXRQ,0) AS 有效日期, " +
                                    "R1.LSJ AS 单价, R1.SYSL AS 剩余数量, R1.SYSL * R1.LSJ AS 剩余金额, R1.PH AS 批号, GYS.MC AS 供应商 FROM ML_RKD0 R0 INNER JOIN ML_RKD1 R1 ON R0.RKDH = R1.RKDH " +
                                     " LEFT JOIN Dic_YP YP ON R1.YPBM = YP.BM LEFT JOIN ML_YPSX SX ON R0.YKBM=SX.YKBM AND R1.YPBM=SX.YPBM " +
                                        "LEFT JOIN Dic_GYS GYS ON R0.GYSBM = GYS.BM WHERE R0.YKBM = '"+ PublicUnit.ConnetionString +"' AND R0.Flag> 0 AND R1.SYSL > 0 ";
                con = new SqlConnection(PublicUnit.ConnetionString);
                sda = new SqlDataAdapter(cmdText,con);
                sda.Fill(ds);
                dgvYPYXQCX.DataSource = ds.Tables[0].DefaultView;
            }
            catch(SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            cbxYPJX.SelectedIndex = -1;
            cbxYPLB.SelectedIndex = -1;
        }

        private void rbJJ_CheckedChanged(object sender, EventArgs e)
        {
            dtpSXRQ.Enabled = false;
            nudJJ.Enabled = true;
        }

        private void rbSXRQ_CheckedChanged(object sender, EventArgs e)
        {
            nudJJ.Enabled = false;
            dtpSXRQ.Enabled = true;
        }

        private void tsbRefresh_Click(object sender, EventArgs e)
        {
            string cmdText = "SELECT R1.YPBM AS 药品编码, YP.MC AS 药品名称, YP.GG AS 规格, YP.BZDW AS 单位, ISNULL(R1.YXRQ,0) AS 有效日期, ";
            if (cbxJEZL.Text == "按零价计算")
                cmdText += "R1.LSJ AS 单价, R1.SYSL AS 剩余数量, R1.SYSL * R1.LSJ AS 剩余金额, R1.PH AS 批号, GYS.MC AS 供应商 FROM ML_RKD0 R0 INNER JOIN ML_RKD1 R1 ON R0.RKDH = R1.RKDH " +
                 " LEFT JOIN Dic_YP YP ON R1.YPBM = YP.BM LEFT JOIN ML_YPSX SX ON R0.YKBM=SX.YKBM AND R1.YPBM=SX.YPBM " +
                    "LEFT JOIN Dic_GYS GYS ON R0.GYSBM = GYS.BM ";
            else if (cbxJEZL.Text == "按批价计算")
                cmdText += "R1.PJ AS 单价, R1.SYSL AS 剩余数量, R1.SYSL * R1.PJ AS 剩余金额, R1.PH AS 批号, GYS.MC AS 供应商 FROM ML_RKD0 R0 INNER JOIN ML_RKD1 R1 ON R0.RKDH = R1.RKDH " +
                 " LEFT JOIN Dic_YP YP ON R1.YPBM = YP.BM LEFT JOIN ML_YPSX SX ON R0.YKBM=SX.YKBM AND R1.YPBM=SX.YPBM " +
                    "LEFT JOIN Dic_GYS GYS ON R0.GYSBM = GYS.BM ";
            else
                cmdText += "R1.JJ AS 单价, R1.SYSL AS 剩余数量, R1.SYSL * R1.JJ AS 剩余金额, R1.PH AS 批号, GYS.MC AS 供应商 FROM ML_RKD0 R0 INNER JOIN ML_RKD1 R1 ON R0.RKDH = R1.RKDH " +
                 " LEFT JOIN Dic_YP YP ON R1.YPBM = YP.BM LEFT JOIN ML_YPSX SX ON R0.YKBM=SX.YKBM AND R1.YPBM=SX.YPBM " +
                    "LEFT JOIN Dic_GYS GYS ON R0.GYSBM = GYS.BM ";
            cmdText += "WHERE R0.YKBM = '" + PublicUnit.UserKSBM + "' AND R0.Flag> 0 AND R1.SYSL > 0 ";
            if (cbxYPJX.Text != "")
                cmdText += "AND YP.JX = "+ Convert.ToByte(cbxYPJX.SelectedValue.ToString().Trim());
            if (cbxYPLB.Text != "")
                cmdText += " AND YP.LX = " + Convert.ToByte(cbxYPJX.SelectedValue.ToString().Trim());
            if (rbSXRQ.Checked)
                cmdText += " AND R1.YXRQ = '" + Convert.ToDateTime(dtpSXRQ.Text.Trim()) + "' ";
            if (rbJJ.Checked)
                cmdText += " AND R1.YXRQ < '" + Convert.ToDateTime(System.DateTime.Now.AddDays(Convert.ToDouble(nudJJ.Value)).ToString().Trim()) + "' ";
            if (rbGQYP.Checked)
                cmdText += " AND R1.YXRQ < '" + Convert.ToDateTime(System.DateTime.Now.ToString().Trim()) + "' ";
            if (rbWGQYP.Checked)
                cmdText += " AND R1.YXRQ > '" + Convert.ToDateTime(System.DateTime.Now.ToString().Trim()) + "' ";
            sda.SelectCommand.CommandText = cmdText;
            sda.Fill(ds);
            dgvYPYXQCX.DataSource = ds.Tables[0].DefaultView;
        }
    }
}
